// pages/recruitBasicInfo/recruitBasicInfo.js
var app=getApp();
var model = require('../../utils/model/model.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: '',
    httpApiImg: app.globalData.httpApiImg,
    formlist: [
      { isrequire: true, label: '姓名', key: 'name', valueType: 'text', tips: '请填写', type: 'input' },
      { isrequire: true, label: '电话', key: 'telephone', maxlength:'11', reg:'^1[34578][0-9]{9}$', valueType: 'number', tips: '请填写', type: 'input'},
      { isrequire: true, label: '职位', key: 'onPosition', index: [0, 0], valueType: 'text', tips: '请填写', type: 'input'},
      { isrequire: true, label: '公司全称', key: 'onCompanyName', valueType: 'text', tips: '请填写', type: 'input'  }
    ],
    formData:{
      name:'',
      headPicture:'',
      telephone: '',
      onPosition: '',
      onCompanyName:''
    },
    canSubmit:false,
    modelData: {
      showModal: false,
      signle: true,
      signleBtnTxt: "确定",
      content: '',
    },
    isEdit:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    model.init.apply(this, ['modelData']);
    this.setData({
      userInfo: JSON.parse(wx.getStorageSync('userInfo'))
    })
    if (options.isEdit){
      this.setData({
        isEdit:true
      })
    }
    this.getUserIdentity();
  },
  nextStep:function(e){
    if (this.data.canSubmit) {
      if (this.checkReg(this.data.formlist) == true) {
        var that = this;
        wx.request({
          url: app.globalData.httpApiCar + 'recruitPersonalInfo/update', //仅为示例，并非真实的接口地址
          method: 'POST',
          header: {
            'content-type': 'application/x-www-form-urlencoded' // 默认值
          },
          data: this.data.formData,
          success: function (res) {
            if (res.data.success) {
              if (!that.data.isEdit){
                wx.redirectTo({
                  url: '/pages/pulishJob/pulishJob',
                })
              }else{
                wx.redirectTo({
                  url: '/pages/recruitMine/recruitMine',
                })
              }
            }
          }
        })
      }
    }
  },
  checkReg: function (data) {
    var flag = true;
    for (var i = 0; i < data.length; i++) {
      if (data[i].reg) {
        var reg = new RegExp(data[i].reg);
        if (!reg.test(this.data.formData[data[i].key])) {
          flag = false;
          this.setData({
            'modelData.showModal': true,
            'modelData.content': '请输入正确的' + data[i].label
          });
          return;
        }
      }
    }
    return flag;
  },
  getUserIdentity: function () {
    var that = this;
    wx.request({
      url: app.globalData.httpApiCar + 'recruitPersonalInfo/getOne', //仅为示例，并非真实的接口地址
      data: { userId: this.data.userInfo.id },
      success: function (res) {
        if (res.data.success) {
          if (res.data.data) {
            for(var j in that.data.formData){
              if (res.data.data[j] !== '' && res.data.data[j] != undefined && res.data.data[j] != null) {
                that.data.formData[j] = res.data.data[j];
              }
            }
            that.data.formData.id = res.data.data.id;
            that.setData({
              myIsRcruit: res.data.data.userCharacter == '招聘' ? true : false,
              formData: that.data.formData
            })
            that.changeValue();
          }
        }
      }
    })
  },
  changeTextarea: function (e) {//输入框值改变事件
    var key = e.target.dataset.value;
    this.data.formData[key] = e.detail.value;
    this.setData({
      formData: this.data.formData
    })
    this.changeValue();
  },
  changeValue() {
    var flag = true;
    var item = this.data.formData;
    if ((item.name == '') || (item.headPicture == '') || (item.telphone == '') || (item.onPosition == '') || (item.onCompanyName == '')) {
      flag = false;
    }
    this.setData({
      canSubmit: flag
    })
  },
  uploadImg: function (e) {
    var that = this;
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      // sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        let key = e.currentTarget.dataset.key;
        var tempFilePaths = res.tempFilePaths;
        that.upload(tempFilePaths);

      }
    })
  },
  /*上传图片*/
  upload: function (path) {
    var that = this;
    wx.showToast({
      icon: "loading",
      title: "正在上传"
    }),
      wx.uploadFile({
        url: app.globalData.httpApiCar + 'uploadfile/upload1',
        filePath: path[0],
        name: 'file',
        header: { "Content-Type": "multipart/form-data" },
        formData: {},
        success: function (res) {
          if (res.statusCode != 200) {
            wx.showModal({
              title: '提示',
              content: '上传失败',
              showCancel: false
            })
            return;
          }
          var url = JSON.parse(res.data).data;
          that.setData({
            'formData.headPicture': url
          })
          that.changeValue();
        },
        fail: function (e) {
          wx.showModal({
            title: '提示',
            content: '上传失败',
            showCancel: false
          })
        },
        complete: function () {
          wx.hideToast();  //隐藏Toast
        }
      })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})